/*
 * For com.royal.art
 * Copyright [2015/11/7] By RICK
 */
package com.manage.controller;

import com.beans.Recommend;
import com.beans.page.PageList;
import com.manage.util.UserUtils;
import com.services.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * IndexController
 * index Controller
 *
 * @author FCHEN
 * @date 2015/11/7
 */
@Controller
@RequestMapping("/config")
public class RecommendControl extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private HouseInfoService houseInfoService;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private NewProjectService newProjectService;
    @Autowired
    private RecommendService recommendService;

    /**
     * 查询列表
     *
     * @return
     */
    @RequiresPermissions("config:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list() {
        return "config/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param recommend
     * @return
     */
    @RequiresPermissions("config:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, Recommend recommend) {
        PageList<Recommend> list = recommendService.queryPage(pageList, recommend);
        for (Recommend item : list.getAaData()) {
            try {
                if (item.getRelationType() == 1) {
                    item.setRelation("首页大图");
                    item.setRelationName(photoService.queryById(item.getRelationId()).getPhotoSource());
                }
                if (item.getRelationType() == 2) {
                    item.setRelation("别墅推荐");
                    item.setRelationName(houseInfoService.queryById(item.getRelationId()).getTitle());
                }
                if (item.getRelationType() == 3) {
                    item.setRelation("豪宅推荐");
                    item.setRelationName(houseInfoService.queryById(item.getRelationId()).getTitle());
                }
                if (item.getRelationType() == 4) {
                    item.setRelation("新楼盘推荐");
                    item.setRelationName(newProjectService.queryById(item.getRelationId()).getProjectName());
                }
                if (item.getRelationType() == 5) {
                    item.setRelation("经纪人推荐");
                    item.setRelationName(sysUserService.queryUserOnlyById(item.getRelationId()).getUsername());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @RequiresPermissions("config:edit")
    @RequestMapping("save")
    @ResponseBody
    public boolean save(Long id, Long relationId) {
        boolean returnValue = false;

        Recommend item = recommendService.queryById(id);
        item.setRelationId(relationId);
        item.setUpdateBy(UserUtils.getUser().getUserId());


        try {
            if (item.getRelationType() == 1) {
                item.setRelation("首页大图");
                item.setRelationName(photoService.queryById(item.getRelationId()).getPhotoSource());
            }
            if (item.getRelationType() == 2) {
                item.setRelation("别墅推荐");
                item.setRelationName(houseInfoService.queryById(item.getRelationId()).getTitle());
            }
            if (item.getRelationType() == 3) {
                item.setRelation("豪宅推荐");
                item.setRelationName(houseInfoService.queryById(item.getRelationId()).getTitle());
            }
            if (item.getRelationType() == 4) {
                item.setRelation("新楼盘推荐");
                item.setRelationName(newProjectService.queryById(item.getRelationId()).getProjectName());
            }
            if (item.getRelationType() == 5) {
                item.setRelation("经纪人推荐");
                item.setRelationName(sysUserService.queryUserOnlyById(item.getRelationId()).getUsername());
            }
        } catch (Exception e) {
            return returnValue;
        }


        int rsout = recommendService.update(item);

        if (rsout > 0) {
            returnValue = true;
        }

        return returnValue;
    }
}
